package com.mystyle.le.weekproject.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.activity.PictureActivity;
import com.mystyle.le.weekproject.adapter.PictureFragmentAdapter;
import com.mystyle.le.weekproject.asynctaks.HeadListAsyncTaks;
import com.mystyle.le.weekproject.asynctaks.PictureAsyncTaks;
import com.mystyle.le.weekproject.beans.PassList;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 资讯页面的图片页面
 * A simple {@link Fragment} subclass.
 */
public class PictureFragment extends Fragment {


    private GridView picture_GV;
    private int index = 0;
    private List<PassList> lists = new ArrayList<>();
    private  PictureFragmentAdapter pictureFragmentAdapter;
    private Button picture_add;
    private boolean isLast = false;

    public PictureFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_picture, container, false);
        //初始化控件
        initView(view);
        //GridView的点击事件
        initItemsChange();
        return view;
    }

    private void initItemsChange() {
        picture_GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), PictureActivity.class);
                intent.putExtra("position",position);
//                Log.e("BBBB","==传递过去的lists=="+lists.size());
                intent.putExtra("list",(Serializable) lists);
                startActivity(intent);
            }
        });
    }

    /*
    * 初始化控件并赋值
    * */
    private void initView(View view) {
        picture_GV = ((GridView) view.findViewById(R.id.picture_GV));
        picture_add = ((Button) view.findViewById(R.id.picture_add));
        pictureFragmentAdapter = new PictureFragmentAdapter(lists, getActivity());
        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {

                List<PassList> list = ParseJson.parsePicJson(path);
                if (list!=null){
                    lists.addAll(list);
                    picture_GV.setAdapter(pictureFragmentAdapter);
                }
            }

        },getActivity()).execute(Url.getPicturePagingPath(index));
        //滑动监听
        picture_GV.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (isLast&&scrollState==SCROLL_STATE_IDLE){
                    picture_add.setVisibility(View.VISIBLE);
                }else {
                    picture_add.setVisibility(View.GONE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isLast = (firstVisibleItem+visibleItemCount)==totalItemCount;
            }
        });


        //加载更多
        picture_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
                    @Override
                    public void sendBack(String path) {
                        List<PassList> list = ParseJson.parsePicJson(path);
                        if (list!=null){
                            lists.addAll(list);
                            pictureFragmentAdapter.notifyDataSetChanged();//刷新适配器
                        }
                    }
                },getActivity()).execute(Url.getPicturePagingPath(index));
            }
        });
    }

}
